import { post, get, getBlob } from '@/utils/http';
import {
  ISupplierListResult,
  ISupplierParams,
  ISupplierSummary,
  ISupplierItem,
  ITraceParams,
  IManufactureResult,
  ISupplierFactorDetail,
  IManufactureItem
} from './model/supplierModel';
import { GetListBomTreeResult } from '@/apis/pcf/myModel/model/modelDetailModel';

import { BasicPagingListResult } from '@/apis/basicModel';

enum Api {
  // 供应商数据集分页
  GET_SUPPLIER_LIST = '/footprint/group/form/pageSupplierDataSet',
  // 导出数据集
  EXPORT_SUPPLIER_FORM = '/footprint/group/form/exportSupplierForm',

  GET_SUMMARY = '/footprint/group/form/summary',
  // 供应商下拉列表
  GET_SUPPLIER_OPTION = '/upms/supplier/listSupplier',
  // 供应商用户下拉列表
  GET_SUPPLIER_USER_OPTION = '/upms/supplierUser/listSupplierUserByNo',
  // 追溯供应商数据
  TRACE_SUPPLIER = '/footprint/modelBom/traceSupplierData',
  // 批量追溯供应商数据
  BATCH_TRACE_SUPPLIER = '/footprint/modelBom/traceBatchSupplierData',

  // 替换供应商数据集
  REPLACE_FORM = '/footprint/modelBom/replaceForm',
  // 零部件场地列表查看
  LIST_MANUFACTURE = '/footprint/supplier/formData/listManufacture',
  // 获取供应商报告因子数据
  SUPPLIER_FACTOR = '/footprint/group/form/getSupplierFactor',
  // 获取供应商BOM
  GET_SUPPLIER_BOM = '/footprint/group/form/listBom',
  // 供应商场地数据
  GET_SUPPLIER_LIST_MANUFACTURE = '/footprint/group/form/listManufacture',
  // 查看零部件模型场地数据
  GET_ITEM_LIST_MANUFACTURE = '/footprint/modelFlow/listManufacture',
  // 根据供应商名称查询供应商
  GET_SUPPLIER_NO = '/upms/supplier/getSupplierByName',
  // 查看输入流场地数据
  GET_FLOW_MANUFACTURE = 'footprint/modelFlow/listPartChangeManufacture'
}

export const getSupplierList = (
  params: ISupplierParams
): Promise<BasicPagingListResult<ISupplierListResult>> => get(Api.GET_SUPPLIER_LIST, params);

// 获取供应商Option
export const getSupplierOptions = (enterpriseNo: string): Promise<ISupplierItem[]> =>
  get(Api.GET_SUPPLIER_OPTION, { enterpriseNo });

export const getSupplierSummary = (): Promise<ISupplierSummary> => get(Api.GET_SUMMARY);

export const getSupplierUserOptions = (supplierNo: string): Promise<any[]> =>
  get(Api.GET_SUPPLIER_USER_OPTION, { supplierNo });

export const traceSupplierData = (params: ITraceParams): Promise<void> =>
  post(Api.TRACE_SUPPLIER, params);

export const batchTraceSupplierData = (
  traceSupplierDataRequests: ITraceParams[],
  modelNo: string
): Promise<void> => post(Api.BATCH_TRACE_SUPPLIER, { traceSupplierDataRequests, modelNo });

export const replaceForm = (nodeNo: string, formNo: string): Promise<void> =>
  post(Api.REPLACE_FORM, { nodeNo, formNo });

export const getManufacture = (formNo: string): Promise<IManufactureResult[]> =>
  get(Api.LIST_MANUFACTURE, { formNo });

export const getSupplierFactor = (formNo: string): Promise<ISupplierFactorDetail> =>
  get(Api.SUPPLIER_FACTOR, { formNo });

export const getSupplierBom = (formNo: string): Promise<GetListBomTreeResult[]> =>
  get(Api.GET_SUPPLIER_BOM, { formNo });

export const getItemListManufacture = (modelNo: string): Promise<any[]> =>
  get(Api.GET_ITEM_LIST_MANUFACTURE, { modelNo });

export const getSupplierListManufacture = (formNo: string): Promise<any[]> =>
  get(Api.GET_SUPPLIER_LIST_MANUFACTURE, { formNo });

export const getFlowManufacture = (nodeNo: string): Promise<IManufactureItem[]> =>
  get(Api.GET_FLOW_MANUFACTURE, { nodeNo });

export const getSupplierNo = (
  supplierName: string,
  enterpriseNo: string,
  supplierCode?: string
): Promise<{
  supplierNo: string;
  supplierName: string;
  supplierCode: string;
}> => get(Api.GET_SUPPLIER_NO, { supplierName, enterpriseNo, supplierCode });

export const getSupplierNoWithNoMsg = (
  supplierName: string,
  enterpriseNo: string,
  supplierCode?: string
): Promise<{
  supplierNo: string;
  supplierName: string;
  supplierCode: string;
}> => get(Api.GET_SUPPLIER_NO, { supplierName, enterpriseNo, supplierCode }, false, false);

export const exportSupplierForm = (params: ISupplierParams) =>
  getBlob(Api.EXPORT_SUPPLIER_FORM, params);
